package com.base.controller;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.voip.Shopinfoes;
import com.base.po.voip.ShopinfoesExample;
import com.base.po.voip.ShopinfoesExample.Criteria;
import com.base.service.LogsService;
import com.base.service.ShopinfoService;
import com.base.utils.IP;
import com.base.utils.Produce;

@Controller
@RequestMapping("/admin")
public class content_tianjiaobada {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private ShopinfoService shopinfoService;

	// 账户信息
	@RequestMapping("/content_tianjiabada")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		if (adminUserId == null) {
			modelAndView.setViewName("jsp/login");
			return modelAndView;
		}
		ShopinfoesExample shopinfoesExample = new ShopinfoesExample();
		shopinfoesExample.setOrderByClause("Grade DESC");
		Criteria a = shopinfoesExample.createCriteria();
		a.andAgentidEqualTo(adminUserId).andTypeEqualTo(1).andDatastatusEqualTo(true);
		System.out.println(adminUserId);
		List<Shopinfoes> shopinfoes = shopinfoService.selectByExample(shopinfoesExample);
		request.setAttribute("shopinfoes", shopinfoes);
        
		modelAndView.setViewName("jsp/content_tianjiabada");
		return modelAndView;
	}
	
	
	String SUCCESS = "success";

	// 添加广告
	@RequestMapping(value = "/insertShopinfoes", method = RequestMethod.POST)
	public void insertShopinfoes(HttpServletRequest request, HttpServletResponse response, 
			String agentid, String logo, String text, String url, 
			Integer type, Boolean datastatus, Integer grade) throws IOException {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String adminId = (String) request.getSession().getAttribute("adminUserId");
		if (adminId == null) {
			out.print("请刷新页面重试！");
			return;
		}	
		
		ShopinfoesExample shopinfoesExample = new ShopinfoesExample();
		shopinfoesExample.setOrderByClause("Grade DESC");
		Criteria a = shopinfoesExample.createCriteria();
		a.andAgentidEqualTo(adminId).andTypeEqualTo(1).andDatastatusEqualTo(true);
		System.out.println(adminId);
		List<Shopinfoes> shopinfoes = shopinfoService.selectByExample(shopinfoesExample);
		System.out.println(shopinfoes.size());
		
		if(shopinfoes.size() == 0) {
			grade = 0;
		}else {
			for(int i=0;i<shopinfoes.size();i++) {
				grade = (shopinfoes.get(0).getGrade())+1;
			}
		}
		
		String id = Produce.getUUID(); 
		int resultCode = shopinfoService.insert(new Shopinfoes(id, adminId, logo, text, url, 1,
				true, grade));
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		if (resultCode == 1) {
			logsService.insert(CommContent.TYPE_UPDATE, "添加"+text+"模块信息", usernamesys,
					true, IP.getIpAddr(request));
			out.print(SUCCESS);
		} else {
			logsService.insert(CommContent.TYPE_UPDATE, "添加"+text+"模块信息失败", usernamesys,
					true, IP.getIpAddr(request));
			out.print("更改失败，请您重试");
		}
	}

}
